
package CCT220_Trabalho04_13106.control;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import CCT220_Trabalho04_13106.model.Disciplina;
import CCT220_Trabalho04_13106.model.Estudante;
import CCT220_Trabalho04_13106.model.Turma;
import CCT220_Trabalho04_13106.view.LimiteTurma;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Trabalho04_13106.control
 * </br>Class: ControleTurma
 * </br>Last Change: 08/10/2011 11:22:59 by marcel
 * </br>Description: Class that controls the operations for the entity
 * Turma
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class ControleTurma {

	/**
	 * ControlePrincipal
	 */
	private final ControlePrincipal ctrPrincipal;
	/**
	 * LimiteTurma
	 */
	private final LimiteTurma       limTurma;
	/**
	 * Vector<Turma>
	 */
	private Vector<Turma>           listaTurma;


	/**
	 * </br>******************************
	 * </br>Constructor for
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>LastChange: 08/10/2011 11:23:02 by marcel
	 * </br>******************************</br>
	 * 
	 * @param controlePrincipal
	 * @throws Exception
	 */
	public ControleTurma(ControlePrincipal controlePrincipal) throws Exception {

		this.ctrPrincipal = controlePrincipal;
		this.limTurma = new LimiteTurma(this);
		this.listaTurma = new Vector<Turma>();
		this.desserializaTurma();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: run
	 * </br>Last Change: 08/10/2011 11:23:03 by marcel
	 * </br>Description: Method that call the menu and manage the users options
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	public void run() throws Exception {

		int option = -1;
		while (true) {
			option = this.limTurma.montaMenu();
			if (option == 5) {
				this.serializaTurma();
				break;
			}
			switch (option) {
				case 1:
					this.cadastra();
					break;
				case 2:
					this.listaUm();
					break;
				case 3:
					this.listaTudo();
					break;
				case 4:
					this.removeUm();
					break;
			}
		}

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: cadastra
	 * </br>Last Change: 08/10/2011 11:23:06 by marcel
	 * </br>Description: Method that call the data insertion and insert the
	 * values in the list
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void cadastra() throws Exception {

		this.adicionaNaLista(limTurma.capturaDadosCadastrais(this.ctrPrincipal
		        .getControleEstudante().getList(), this.ctrPrincipal
		        .getControleDisciplina().getList()));
		this.serializaTurma();

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: listaUm
	 * </br>Last Change: 08/10/2011 11:23:08 by marcel
	 * </br>Description: Method that show just one element from the list
	 * </br>******************************</br>
	 * 
	 */
	private void listaUm() {

		int selectedOne = -1;
		do {
			selectedOne = this.limTurma.selectOne(this.getList());
		} while (!this.isInList(selectedOne));
		this.limTurma.show(this.getFromList(selectedOne).toString());

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: listaTudo
	 * </br>Last Change: 08/10/2011 11:23:10 by marcel
	 * </br>Description: Method that show all elements from the list
	 * </br>******************************</br>
	 * 
	 */
	private void listaTudo() {

		this.limTurma.showAll(this.getList());

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: removeUm
	 * </br>Last Change: 08/10/2011 11:23:12 by marcel
	 * </br>Description: Method that removes one element from the list
	 * </br>******************************</br>
	 * 
	 */
	private void removeUm() {

		Integer selectedOne = -1;
		do {
			selectedOne = this.limTurma.selectOne(this.getList());
		} while (!this.isInList(selectedOne));
		if (selectedOne != null) {
			String description = this.getFromList(selectedOne).toString();
			this.listaTurma.remove(this.getFromList(selectedOne));
			this.limTurma.show(description + " foi removida!");
		}
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: adicionaNaLista
	 * </br>Last Change: 08/10/2011 11:23:15 by marcel
	 * </br>Description: Method that add an element to the list
	 * </br>******************************</br>
	 * 
	 * @param t
	 */
	private void adicionaNaLista(Turma t) {

		this.listaTurma.add(t);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: getList
	 * </br>Last Change: 08/10/2011 11:23:17 by marcel
	 * </br>Description: Method that get the whole list in a
	 * LinkedList<String> form
	 * </br>******************************</br>
	 * 
	 * @return LinkedList<String>
	 */
	private LinkedList<String> getList() {

		LinkedList<String> lista = new LinkedList<String>();
		Iterator<Turma> it = this.listaTurma.iterator();
		while (it.hasNext()) {
			lista.add(it.next().toString());
		}
		return lista;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: isInList
	 * </br>Last Change: 08/10/2011 11:23:18 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list
	 * </br>******************************</br>
	 * 
	 * @param selectedOne
	 * @return true | false
	 */
	private boolean isInList(int selectedOne) {

		Iterator<Turma> it = this.listaTurma.iterator();
		while (it.hasNext()) {
			if (it.next().getCodigoTurma() == selectedOne) { return true; }
		}
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: getFromList
	 * </br>Last Change: 08/10/2011 11:23:20 by marcel
	 * </br>Description: Method that get one element from the list by the given
	 * selected element code
	 * </br>******************************</br>
	 * 
	 * @param selectedOne
	 * @return Turma
	 */
	private Turma getFromList(int selectedOne) {

		Iterator<Turma> it = this.listaTurma.iterator();
		while (it.hasNext()) {
			Turma t = it.next();
			if (t.getCodigoTurma() == selectedOne) { return t; }
		}
		return null;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: isInListDisciplina
	 * </br>Last Change: 08/10/2011 11:23:22 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list of Disciplina
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return true | false
	 */
	public boolean isInListDisciplina(int choice) {

		if (ctrPrincipal.getControleDisciplina().getFromList(choice) != null) { return true; }
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: getInListDisciplina
	 * </br>Last Change: 08/10/2011 11:23:24 by marcel
	 * </br>Description: Method get the element Disciplina by the given selected
	 * element code is in the list of Disciplina
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return Disciplina
	 */
	public Disciplina getInListDisciplina(int choice) {

		return ctrPrincipal.getControleDisciplina().getFromList(choice);
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: isInListEstudante
	 * </br>Last Change: 08/10/2011 11:23:26 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list of Estudante
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return true | false
	 */
	public boolean isInListEstudante(int choice) {

		if (ctrPrincipal.getControleEstudante().getFromList(choice) != null) { return true; }
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: getInListEstudante
	 * </br>Last Change: 08/10/2011 11:23:28 by marcel
	 * </br>Description: Method get the element Estudante by the given selected
	 * element code is in the list of Estudante
	 * </br>******************************</br>
	 * 
	 * @param choice
	 * @return Estudante
	 */
	public Estudante getInListEstudante(int choice) {

		return ctrPrincipal.getControleEstudante().getFromList(choice);
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: serializaTurma
	 * </br>Last Change: 08/10/2011 11:23:30 by marcel
	 * </br>Description: Method that serializes the list of elements to a file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void serializaTurma() throws Exception {

		FileOutputStream fos = new FileOutputStream("turmas.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(this.listaTurma);
		oos.flush();
		oos.close();

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleTurma
	 * </br>Method: desserializaTurma
	 * </br>Last Change: 08/10/2011 11:23:32 by marcel
	 * </br>Description: Method that desserializes the list of elements to a
	 * file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private void desserializaTurma() throws Exception {

		File file = new File("turmas.dat");
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			this.listaTurma = (Vector<Turma>) ois.readObject();
			ois.close();
		}

	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#finalize()
	 */
	@Override
	public void finalize() throws Exception {

		this.serializaTurma();
	}

}
